package com.senior.sendmessage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senior.sendmessage.common.CommonEnum;
import com.senior.sendmessage.constant.CommonConstant;
import com.senior.sendmessage.entity.CalendarResultDTO;
import com.senior.sendmessage.entity.MessageLogPO;
import com.senior.sendmessage.mapper.MessageLogMapper;
import com.senior.sendmessage.service.CalendarService;
import com.senior.sendmessage.service.MessageLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * (MessageLog)表服务实现类
 *
 * @author 白纪良
 * @since 2021-11-04 14:55:56
 */
@Service
@RequiredArgsConstructor
public class MessageLogServiceImpl extends ServiceImpl<MessageLogMapper, MessageLogPO> implements MessageLogService {
    private final CalendarService calendarService;

    @Override
    public MessageLogPO getMessageLog(final String date) {
        final CalendarResultDTO calendar = calendarService.getCalendar(date);
        if (0 != calendar.getErrorCode()) {
            return MessageLogPO.builder().status(CommonConstant.OUT_FAILED_CODE).statusDesc(CommonEnum.OUT_FAILED.getMessage()).sendTime(LocalDateTime.now()).build();
        }

        return MessageLogPO.builder()
                .week(calendar.getResult().getWeek())
                .sendTime(LocalDateTime.now())
                .status(calendar.getResult().getStatus())
                .statusDesc(calendar.getResult().getStatusDesc())
                .description(calendar.getResult().getDesc())
                .suit(calendar.getResult().getSuit())
                .avoid(calendar.getResult().getAvoid())
                .gzYear(calendar.getResult().getGzYear())
                .lMonth(calendar.getResult().getLMonth())
                .lDate(calendar.getResult().getLDate())
                .animal(calendar.getResult().getAnimal())
                .workDesc(calendarService.isCalendar(calendar.getResult().getStatusDesc()))
                .build();
    }
}
